package com.filter;

import com.bean.BhUser;
import com.bean.Bhcase;
import com.dao.BhcaseDao;
import com.dao.impl.BhcaseDaoImpl;
import com.util.Timeone;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebFilter("/BhCase/*")
public class BhSyotFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        BhcaseDao bhcaseDao = new BhcaseDaoImpl();
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String url = request.getRequestURI();
        String name = url.replaceFirst("/BhSyCase","");
        System.out.println("dsadsa");
        HttpSession session = request.getSession();
        if(request.getParameter("a")!=null){
            System.out.println("az");
            chain.doFilter(req,resp);
            return;
        }
        if (session.getAttribute("BhSyUser")!=null && !"".equals(session.getAttribute("BhSyUser"))) {
            List<Bhcase> list = null;
            try {
                list = bhcaseDao.queryBhcase("SELECT * FROM  bhcase where cUrl='" + name + "'");
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (list != null && list.size()!=0) {
                request.setAttribute("list", list);
                List list1 = null;
                try {
                    BhUser bhUser= (BhUser) session.getAttribute("BhSyUser");
                    list1=bhcaseDao.queryBhcase("SELECT * FROM  usecasetrue where cUrl='" + name + "' and uID="+bhUser.getuID());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(list1!=null && list1.size()!=0){
                    request.getRequestDispatcher("/cheng/CheTao?url="+list.get(0).getcUrl()).forward(request, response);
                    return;
                }else {
                    if (Timeone.ka(list.get(0).getcBegin(), list.get(0).getcEnd()) == 0) {

                        request.getRequestDispatcher("../UsecasetrueServlet/Usecaet").forward(request, response);
                        return;
                    } else {
                        request.getRequestDispatcher("/kaoqian/uo").forward(request, response);
                        return;
                    }
                }}else {
                    request.getRequestDispatcher("/index.jsp").forward(request, response);
                    return;
                }
        }else {
            session.setAttribute("kao",url);
            request.getRequestDispatcher("/WEB-INF/jsp/BhSyCaseJoin.jsp").forward(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
